package com.github.skystardust.lotteryex.lottery.bundle;

import com.github.skystardust.lotteryex.lottery.item.AwardItem;
import com.github.skystardust.lotteryex.lottery.item.api.Award;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

@EqualsAndHashCode(of = "uuid")
@ToString
public class AwardBundle implements Award<AwardBundle> {
    private UUID uuid;
    private List<AwardItem> awardItemList;

    public AwardBundle(List<AwardItem> awardItemList, ItemStack displayItemStack) {
        this(UUID.randomUUID(), awardItemList);
    }

    public AwardBundle(UUID uuid, List<AwardItem> awardItemList) {
        this.uuid = uuid;
        this.awardItemList = awardItemList;
    }

    public AwardBundle(Map<String, Object> stringObjectMap) {
        this.uuid = UUID.fromString(((String) stringObjectMap.get("uuid")));
        this.awardItemList = (List<AwardItem>) stringObjectMap.get("awardItemList");
    }

    private AwardBundle(Builder builder) {
        setUuid(builder.uuid);
        setAwardItemList(builder.awardItemList);
    }


    public static Builder newBuilder() {
        return new Builder();
    }

    public UUID getUuid() {
        return uuid;
    }

    public void setUuid(UUID uuid) {
        this.uuid = uuid;
    }

    public List<AwardItem> getAwardItemList() {
        return awardItemList;
    }

    public void setAwardItemList(List<AwardItem> awardItemList) {
        this.awardItemList = awardItemList;
    }

    @Override
    public void giveAward(Player player) {
        getAwardItemList().forEach(awardItem -> awardItem.giveAward(player));
    }


    @Override
    public Map<String, Object> serialize() {
        TreeMap<String, Object> stringObjectTreeMap = new TreeMap<>();
        stringObjectTreeMap.put("uuid", uuid.toString());
        stringObjectTreeMap.put("awardItemList", awardItemList);
        return stringObjectTreeMap;
    }

    public static final class Builder {
        private UUID uuid;
        private List<AwardItem> awardItemList;

        private Builder() {
        }

        @Nonnull
        public Builder withUuid(@Nonnull UUID val) {
            uuid = val;
            return this;
        }

        @Nonnull
        public Builder withAwardItemList(@Nonnull List<AwardItem> val) {
            awardItemList = val;
            return this;
        }

        @Nonnull
        public AwardBundle build() {
            return new AwardBundle(this);
        }
    }
}
